package clueless.server;

import static org.junit.Assert.*;

import org.junit.Test;

import clueless.utilities.Card;
import clueless.utilities.Connection;
import clueless.utilities.Map;
import clueless.utilities.Room;
import clueless.utilities.Suggestion;
import clueless.utilities.Character;
import clueless.utilities.Weapon;

public class GameTest {

	@Test
	public void testDistCards() 
	{
		Game game = new Game();
		
		Player mrGreen = new Player(Character.m_MR_GREEN, new Connection());
		game.addPlayer(mrGreen);
		
		Player profPlum = new Player(Character.m_PROF_PLUM, new Connection());
		game.addPlayer(profPlum);
		
		Player mrsWhite = new Player(Character.m_MRS_WHITE, new Connection());
		game.addPlayer(mrsWhite);
		
		game.distributeCards();
		
		assertEquals(mrGreen.numCards()+profPlum.numCards()+mrsWhite.numCards()+3, Character.m_numCharacters+Weapon.m_numWeapons+Room.m_numRooms);

	}
	
	@Test
	public void testAddPlayers() 
	{
		Game game = new Game();
		
		Player mrGreen = new Player(Character.m_MR_GREEN, new Connection());
		game.addPlayer(mrGreen);
		assertEquals(game.getNumPlayers(),1);
		assertFalse(game.enoughPlayers());
		
		Player profPlum = new Player(Character.m_PROF_PLUM, new Connection());
		game.addPlayer(profPlum);
		assertEquals(game.getNumPlayers(),2);
		assertFalse(game.enoughPlayers());
		
		Player mrsWhite = new Player(Character.m_MRS_WHITE, new Connection());
		game.addPlayer(mrsWhite);
		assertEquals(game.getNumPlayers(),3);
		assertTrue(game.enoughPlayers());
	}
	
	@Test
	public void testInitPlayerLocation() {
		Game game = new Game();
		Map map = game.getGameMap();
		
		Player mrGreen = new Player(Character.m_MR_GREEN, new Connection());
		game.addPlayer(mrGreen);
		assertEquals(mrGreen.getRoom(), map.getConservatory().getRightRoom());
		
		Player mrsWhite = new Player(Character.m_MRS_WHITE, new Connection());
		game.addPlayer(mrsWhite);
		assertEquals(mrsWhite.getRoom(), map.getBallroom().getRightRoom());
		
		Player mrsPeakcock = new Player(Character.m_MRS_PEACOCK, new Connection());
		game.addPlayer(mrsPeakcock);
		assertEquals(mrsPeakcock.getRoom(), map.getConservatory().getUpRoom());
		
		Player profPlum = new Player(Character.m_PROF_PLUM, new Connection());
		game.addPlayer(profPlum);
		assertEquals(profPlum.getRoom(), map.getLibrary().getUpRoom());
		
		Player msScarlet = new Player(Character.m_MISS_SCARLET, new Connection());
		game.addPlayer(msScarlet);
		assertEquals(msScarlet.getRoom(), map.getLounge().getLeftRoom());
		
		Player colMustart = new Player(Character.m_COL_MUSTARD, new Connection());
		game.addPlayer(colMustart);
		assertEquals(colMustart.getRoom(), map.getLounge().getDownRoom());
	}
	
	@Test
	public void testNextPlayerTurn() {
		Game game = new Game();
		
		Player mrGreen = new Player(Character.m_MR_GREEN, new Connection());
		game.addPlayer(mrGreen);
		
		Player mrsWhite = new Player(Character.m_MRS_WHITE, new Connection());
		game.addPlayer(mrsWhite);
		
		Player mrsPeacock = new Player(Character.m_MRS_PEACOCK, new Connection());
		game.addPlayer(mrsPeacock);
		
		assertEquals(game.getCurrentPlayer(), mrGreen);
		assertEquals(game.getPlayer(0), mrGreen);
		assertEquals(game.getPlayer(1), mrsWhite);
		assertEquals(game.getPlayer(2), mrsPeacock);
		assertEquals(game.getPlayer(3), mrGreen);
		assertEquals(game.getNextActivePlayer(), mrsWhite);
		
		
		game.endTurn();
		
		assertEquals(game.getCurrentPlayer(), mrsWhite);
		assertEquals(game.getPlayer(0), mrsWhite);
		assertEquals(game.getPlayer(1), mrsPeacock);
		assertEquals(game.getPlayer(2), mrGreen);
		assertEquals(game.getPlayer(3), mrsWhite);
		assertEquals(game.getNextActivePlayer(), mrsPeacock);
		
		game.endTurn();
		
		assertEquals(game.getCurrentPlayer(), mrsPeacock);
		assertEquals(game.getPlayer(0), mrsPeacock);
		assertEquals(game.getPlayer(1), mrGreen);
		assertEquals(game.getPlayer(2), mrsWhite);
		assertEquals(game.getPlayer(3), mrsPeacock);
		assertEquals(game.getNextActivePlayer(), mrGreen);
		
		mrGreen.setIsStillPlaying(false);
		assertEquals(game.getNextActivePlayer(), mrsWhite);
		game.endTurn();
		
		assertEquals(game.getCurrentPlayer(), mrsWhite);
		assertEquals(game.getPlayer(0), mrsWhite);
		assertEquals(game.getPlayer(1), mrsPeacock);
		assertEquals(game.getPlayer(2), mrGreen);
		assertEquals(game.getPlayer(3), mrsWhite);
		
	}
	
	@Test
	public void testVerifyAccusation() {
		Game game = new Game();
		
		Player mrGreen = new Player(Character.m_MR_GREEN, new Connection());
		game.addPlayer(mrGreen);
		
		Player mrsWhite = new Player(Character.m_MRS_WHITE, new Connection());
		game.addPlayer(mrsWhite);
		
		Player mrsPeakcock = new Player(Character.m_MRS_PEACOCK, new Connection());
		game.addPlayer(mrsPeakcock);
		
		game.solution[0] = new Card("character", Character.m_COL_MUSTARD);
		game.solution[1] = new Card("weapon", Weapon.m_CANDLESTICK);
		game.solution[2] = new Card("room", Room.m_BALLROOM);
		
		Suggestion suggestion = new Suggestion(Character.m_COL_MUSTARD, Weapon.m_CANDLESTICK, Room.m_BALLROOM);
		
		Player currentPlayer = game.getCurrentPlayer();
		assertTrue(currentPlayer.getIsStillPlaying());
		
		assertTrue(game.verifyAccusation(suggestion));
		assertTrue(currentPlayer.getIsStillPlaying());
		
		suggestion = new Suggestion(Character.m_COL_MUSTARD.toLowerCase(), Weapon.m_CANDLESTICK.toLowerCase(), Room.m_BALLROOM.toLowerCase());
		assertTrue(game.verifyAccusation(suggestion));
		assertTrue(currentPlayer.getIsStillPlaying());
		
		suggestion = new Suggestion(Character.m_COL_MUSTARD, Weapon.m_CANDLESTICK, Room.m_KITCHEN);
		assertFalse(game.verifyAccusation(suggestion));
		assertFalse(currentPlayer.getIsStillPlaying());
	}

}
